$(function() {
				var $form_div = $("#monthTotal_form div");
				winResize($form_div);
				$(window).resize(function(){//当调整浏览器窗口的大小时，发生 resize 事件
					winResize($form_div);
				});
				loadTable();
				
//				var date=new Date;
//				var year=date.getFullYear(); 
//				$('#year_date').numberspinner('setValue', year);   
				
})


var $form = $('#monthTotal_form');

function  loadTable()
{	
	var columnsAll = new Array();
	columnsAll.push({field:'ids',title:'ids',align:'center',rowspan:2,hidden:true});
	columnsAll.push({field:'date',title:'月份',align:'center',rowspan:2});
	columnsAll.push({field:'xzrs',title:'新增人数',align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		},rowspan:2});
	columnsAll.push({field:'lcs',title:'疗程数',align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		},rowspan:2});
	
	
	columnsAll.push({field:'fwcs',title:'服务次数',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	columnsAll.push({field:'fwrs',title:'服务人数',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	
	columnsAll.push({field:'sjjzjs',title:'实际<br/>减重斤数',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	columnsAll.push({field:'xgkyjjs',title:'当年当月<br/>新增顾客<br/>应减斤数',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	columnsAll.push({field:'dndyxgksjjs',title:'当年新增<br/>顾客当月<br/>实减斤数',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	columnsAll.push({field:'ljjzl',title:'累计<br/>减重率',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value!=null){ return  value.toFixed(2)+"%";}
		}
	});

	columnsAll.push({field:'dbrs',title:'达标<br/>人数',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	
	columnsAll.push({field:'jzzxse',title:'减重销售',rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	columnsAll.push({field:'cpxs',title:'产品销售',colspan:10,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	
	columnsAll.push({field:'pricetotal',title:' 总销售  ',width:65,rowspan:2,align:'center',
		formatter: function(value,row,index)
		{	
			if(value == "0" || value == "0.0"){
				return  "";
			}else{
				return value;
			}
			
		}});
	
	
	var columnsAll1 = new Array();
	
	
	columnsAll1.push({field:'hf001salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'hf002salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'hf003salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'hf004salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'hf005salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'hf011salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'ss001salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'ss002salprice',title:'金额',width:50,align:'center',hidden:true});
	columnsAll1.push({field:'jyg',title:'精油、膏',width:65,align:'center',
		formatter: function(value,row,index)
		{	
			var jyg = parseFloat(row.ss001salprice)+parseFloat(row.ss002salprice);
			if(jyg == "0"){
				return "";
			}else{
				return  jyg.toFixed(1);
			}
			
		}
	});
	columnsAll1.push({field:'qt',title:'其他产品',width:60,align:'center',
		formatter: function(value,row,index)
		{	
			var qt = parseFloat(row.hf001salprice)+parseFloat(row.hf002salprice)
			+parseFloat(row.hf003salprice)
			+parseFloat(row.hf004salprice)
			+parseFloat(row.hf005salprice)
			+parseFloat(row.hf011salprice);
			if(qt == "0"){
				return "";
			}else{
				return  qt.toFixed(1);
			}
			
		}
	});
	
	
	$('#monthTotal_datagrid').datagrid({
		cls: "theme-datagrid",
		singleSelect:true,
		fitColumns:false,
		rownumbers: true,
	    columns:[columnsAll,columnsAll1],
	    url: "monthTotal/getMonthTotal",
	    queryParams: sy.serializeObject($form),
	    onLoadSuccess:function(data){
			var n = data.total;
			var myDate = new Date();
			var date = myDate.getDate(); 
			var month = myDate.getMonth();
			var mm = 30;
			if(month == '0' ||month == '2'||month == '4'||month == '6'||month == '7'||month == '9'||month == '11'){
				m = 31;
			}
			 var rows = $('#monthTotal_datagrid').datagrid('getRows');
			if(rows.length > 0){
				var xgkyjjssum = compute_GXC('xgkyjjs');
				var xgksjjs = compute_GXC('dndyxgksjjs');
				var xzrs = compute_GXC('xzrs');
				var lcs = compute_GXC('lcs');
				var fwcs = compute_GXC('fwcs');
				var fwrs = compute_GXC('fwrs');
				var sjjzjs = compute_GXC('sjjzjs');
				var xgkyjjs = compute_GXC('xgkyjjs');
				var dndyxgksjjs = compute_GXC('dndyxgksjjs');
				var dbrs = compute_GXC('dbrs');
				var jzzxse = compute_GXC('jzzxse');
				var hf001salnum = compute_GXC('hf001salnum');
				var hf001salprice = compute_GXC('hf001salprice');
				var hf002salnum = compute_GXC('hf002salnum');
				var hf002salprice = compute_GXC('hf002salprice');
				var hf003salnum = compute_GXC('hf003salnum');
				var hf003salprice = compute_GXC('hf003salprice');
				var hf004salnum = compute_GXC('hf004salnum');
				var hf004salprice = compute_GXC('hf004salprice');
				var hf005salnum = compute_GXC('hf005salnum');
				var hf005salprice = compute_GXC('hf005salprice');
				var hf011salnum = compute_GXC('hf011salnum');
				var hf011salprice = compute_GXC('hf011salprice');
				
				var ss001salnum = compute_GXC('ss001salnum');
				var ss001salprice = compute_GXC('ss001salprice');
				var ss002salnum = compute_GXC('ss002salnum');
				var ss002salprice = compute_GXC('ss002salprice');
				
				
				var pricetotal = compute_GXC('pricetotal');
				
				
				
				var xzrs1 = compute_GXC1('xzrs');
				var lcs1 = compute_GXC1('lcs');
				var fwcs1 = compute_GXC1('fwcs');
				var fwrs1 = compute_GXC1('fwrs');
				var sjjzjs1 = compute_GXC1('sjjzjs');
				var xgkyjjs1 = compute_GXC1('xgkyjjs');
				var dndyxgksjjs1 = compute_GXC1('dndyxgksjjs');
				var dbrs1 = compute_GXC1('dbrs');
				var jzzxse1 = compute_GXC1('jzzxse');
				var hf001salnum1 = compute_GXC1('hf001salnum');
				var hf001salprice1 = compute_GXC1('hf001salprice');
				var hf002salnum1 = compute_GXC1('hf002salnum');
				var hf002salprice1 = compute_GXC1('hf002salprice');
				var hf003salnum1 = compute_GXC1('hf003salnum');
				var hf003salprice1 = compute_GXC1('hf003salprice');
				var hf004salnum1 = compute_GXC1('hf004salnum');
				var hf004salprice1 = compute_GXC1('hf004salprice');
				var hf005salnum1 = compute_GXC1('hf005salnum');
				var hf005salprice1 = compute_GXC1('hf005salprice');
				var hf011salnum1 = compute_GXC1('hf011salnum');
				var hf011salprice1 = compute_GXC1('hf011salprice');
				
				var ss001salnum1 = compute_GXC1('ss001salnum');
				var ss001salprice1 = compute_GXC1('ss001salprice');
				var ss002salnum1 = compute_GXC1('ss002salnum');
				var ss002salprice1 = compute_GXC1('ss002salprice');
				
				
				var pricetotal1 = compute_GXC1('pricetotal');
				var m = 0;
				if(xgkyjjssum!=0){
					m = parseFloat(xgksjjs)/parseFloat(xgkyjjssum) * 100;
					
				}
				$('#monthTotal_datagrid').datagrid('insertRow', {
					index: 0,
					row: {date: '合计',
					xzrs:xzrs,
					lcs:lcs,
					fwcs:fwcs,
					fwrs:fwrs,
					sjjzjs:sjjzjs,
					xgkyjjs:xgkyjjs,
					dndyxgksjjs:dndyxgksjjs,
					ljjzl:m,
					dbrs:dbrs,
					jzzxse:jzzxse,
					hf001salnum:hf001salnum,
					hf001salprice:hf001salprice,
					hf002salnum:hf002salnum,
					hf002salprice:hf002salprice,
					hf003salnum:hf003salnum,
					hf003salprice:hf003salprice,
					hf004salnum:hf004salnum,
					hf004salprice:hf004salprice,
					hf005salnum:hf005salnum,
					hf005salprice:hf005salprice,
					hf011salnum:hf011salnum,
					hf011salprice:hf011salprice,
					
					ss001salnum:ss001salnum,
					ss001salprice:ss001salprice,
					ss002salnum:ss002salnum,
					ss002salprice:ss002salprice,
					
					
					pricetotal:pricetotal
					}
	             });
				
				$('#monthTotal_datagrid').datagrid('insertRow', {
					index: 1,
					row: {date: '月均',
					xzrs:(((xzrs1/(n-1))+((xzrs-xzrs1)/date)*mm)/n).toFixed(1),
					lcs:(((lcs1/(n-1))+((lcs-lcs1)/date)*mm)/n).toFixed(1),
					fwcs:(((fwcs1/(n-1))+((fwcs-fwcs1)/date)*mm)/n).toFixed(1),
					fwrs:(((fwrs1/(n-1))+((fwrs-fwrs1)/date)*mm)/n).toFixed(1),
					sjjzjs:(((sjjzjs1/(n-1))+((sjjzjs-sjjzjs1)/date)*mm)/n).toFixed(1),
					xgkyjjs:(((xgkyjjs1/(n-1))+((xgkyjjs-xgkyjjs1)/date)*mm)/n).toFixed(1),
					dndyxgksjjs:(((dndyxgksjjs1/(n-1))+((dndyxgksjjs-dndyxgksjjs1)/date)*mm)/n).toFixed(1),
					ljjzl:m,
					dbrs:(((dbrs1/(n-1))+((dbrs-dbrs1)/date)*mm)/n).toFixed(1),
					jzzxse:(((jzzxse1/(n-1))+((jzzxse-jzzxse1)/date)*mm)/n).toFixed(1),
					hf001salnum:(((hf001salnum1/(n-1))+((hf001salnum-hf001salnum1)/date)*mm)/n).toFixed(1),
					hf001salprice:(((hf001salprice1/(n-1))+((hf001salprice-hf001salprice1)/date)*mm)/n).toFixed(1),
					hf002salnum:(((hf002salnum1/(n-1))+((hf002salnum-hf002salnum1)/date)*mm)/n).toFixed(1),
					hf002salprice:(((hf002salprice1/(n-1))+((hf002salprice-hf002salprice1)/date)*mm)/n).toFixed(1),
					hf003salnum:(((hf003salnum1/(n-1))+((hf003salnum-hf003salnum1)/date)*mm)/n).toFixed(1),
					hf003salprice:(((hf003salprice1/(n-1))+((hf003salprice-hf003salprice1)/date)*mm)/n).toFixed(1),
					hf004salnum:(((hf004salnum1/(n-1))+((hf004salnum-hf004salnum1)/date)*mm)/n).toFixed(1),
					hf004salprice:(((hf004salprice1/(n-1))+((hf004salprice-hf004salprice1)/date)*mm)/n).toFixed(1),
					hf005salnum:(((hf005salnum1/(n-1))+((hf005salnum-hf005salnum1)/date)*mm)/n).toFixed(1),
					hf005salprice:(((hf005salprice1/(n-1))+((hf005salprice-hf005salprice1)/date)*mm)/n).toFixed(1),
					hf011salnum:(((hf011salnum1/(n-1))+((hf011salnum-hf011salnum1)/date)*mm)/n).toFixed(1),
					hf011salprice:(((hf011salprice1/(n-1))+((hf011salprice-hf011salprice1)/date)*mm)/n).toFixed(1),
					
					ss001salnum:(((ss001salnum1/(n-1))+((ss001salnum-ss001salnum1)/date)*mm)/n).toFixed(1),
					ss001salprice:(((ss001salprice1/(n-1))+((ss001salprice-ss001salprice1)/date)*mm)/n).toFixed(1),
					ss002salnum:(((ss002salnum1/(n-1))+((ss002salnum-ss002salnum1)/date)*mm)/n).toFixed(1),
					ss002salprice:(((ss002salprice1/(n-1))+((ss002salprice-ss002salprice1)/date)*mm)/n).toFixed(1),
					
					
					pricetotal:(((pricetotal1/(n-1))+((pricetotal-pricetotal1)/date)*mm)/n).toFixed(1)
					}
	             });
		
			} 
		}
	});
}
//指定列求和
function compute_GXC(colName) {
     var rows = $('#monthTotal_datagrid').datagrid('getRows');
     var total = 0;
     for (var i = 0; i < rows.length; i++) {
    	 if(typeof(rows[i][colName]) != 'undefined'){
    		 total += parseFloat(rows[i][colName]);
    	 }
     }
     if(total > 0){
    	 return total.toFixed(1);
     }else{
    	 return 0;
     }
}

//除去最后一列求和
function compute_GXC1(colName) {
     var rows = $('#monthTotal_datagrid').datagrid('getRows');
     var total = 0;
     for (var i = 0; i < rows.length-1; i++) {
    	 if(typeof(rows[i][colName]) != 'undefined'){
    		 total += parseFloat(rows[i][colName]);
    	 }
     }
     if(total > 0){
    	 return total.toFixed(1);
     }else{
    	 return 0;
     }
}
function searchFrom(){
	$('#monthTotal_datagrid').datagrid('load',sy.serializeObject($form));
}


